// 添加类名
export function addClass(el, className) {
  if( hasClass( el, className ) ) {
    return
  }
  let newClass = el.className.split(' ')
  newClass.push(className)
  el.className = newClass.join(' ')
}

// 检测类名
export function hasClass(el, className) {
  let reg = new RegExp('(^|\\s)'+ className + '(\\s|$)')
  return reg.test( el.className )
}

export function getData(ele, name, val) {
  const prefix = 'data-'
  name = prefix + name
  if( val ) {
    return ele.setAttribute(name,val)
  } else {
    return ele.getAttribute(name)
  }
}

let elementStyle = document.createElement('div').style

let vendor = (() => {
  let transformNames = {
    webkit: 'webkitTransform',
    Moz: 'MozTransform',
    O: 'OTransform',
    ms: 'msTransform',
    standard: 'transform'
  }

  for( let key in transformNames ) {
    if( elementStyle[transformNames[key]] !== undefined ) {
      return key
    }
  }

  return false
})()

// 添加浏览器头信息
export function prefixStyle(style) {
  if( vendor === false ) return false
  if( vendor === 'standard' ) return style
  return vendor + style.charAt(0).toUpperCase() + style.substr(1) 
}

// 清除节点空隙 
export function clearGap(parent, children) {
  ...children.forEach(childNode => {
    if(childNode.nodeType === 3) {
      parent.removeChild(childNode)
    }
  });
}